package cn.lanqiao.ui;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.UUID;

import javax.swing.JTextField;

import cn.lanqiao.entity.Student;
import cn.lanqiao.service.UserService;
import cn.lanqiao.service.impl.UserServiceImpl;
import java.awt.Font;

public class AddStudent extends JDialog {
	private static final long serialVersionUID = 7504229208690613537L;
	private JTextField txtSid;
	private JTextField txtSname;
	private JTextField txtSgender;
	private JTextField txtScollege;
	private JTextField txtSmajor;
	private JTextField txtSclass;
	private JTextField txtSrol;

	public static void main(String[] args) {
		try {
			AddStudent dialog = new AddStudent();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	StudentInformation frm=null;
	public AddStudent(StudentInformation frm) {
		this();
		this.frm = frm;
		
	}
	public AddStudent() {
		setTitle("学籍录入");
		setResizable(false);
		setBounds(100, 100, 368, 388);
		this.setLocationRelativeTo(null);
		getContentPane().setLayout(null);

		JLabel label = new JLabel("学号");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label.setBounds(61, 45, 72, 18);
		getContentPane().add(label);

		txtSid = new JTextField();
		txtSid.setBounds(144, 45, 86, 24);
		getContentPane().add(txtSid);
		txtSid.setColumns(10);

		JLabel label_1 = new JLabel("姓名");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_1.setBounds(61, 76, 72, 18);
		getContentPane().add(label_1);

		txtSname = new JTextField();
		txtSname.setBounds(144, 79, 86, 24);
		getContentPane().add(txtSname);
		txtSname.setColumns(10);

		JLabel label_2 = new JLabel("性别");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_2.setBounds(61, 107, 72, 18);
		getContentPane().add(label_2);

		txtSgender = new JTextField();
		txtSgender.setBounds(144, 110, 86, 24);
		getContentPane().add(txtSgender);
		txtSgender.setColumns(10);

		JLabel label_3 = new JLabel("学院");
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_3.setBounds(61, 135, 72, 18);
		getContentPane().add(label_3);

		txtScollege = new JTextField();
		txtScollege.setBounds(144, 141, 131, 24);
		getContentPane().add(txtScollege);
		txtScollege.setColumns(10);

		JLabel label_4 = new JLabel("专业");
		label_4.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_4.setBounds(61, 166, 72, 18);
		getContentPane().add(label_4);

		txtSmajor = new JTextField();
		txtSmajor.setBounds(144, 169, 86, 24);
		getContentPane().add(txtSmajor);
		txtSmajor.setColumns(10);

		JLabel label_5 = new JLabel("班级");
		label_5.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_5.setBounds(61, 201, 72, 18);
		getContentPane().add(label_5);

		txtSclass = new JTextField();
		txtSclass.setBounds(144, 200, 86, 24);
		getContentPane().add(txtSclass);
		txtSclass.setColumns(10);

		JLabel label_6 = new JLabel("入学时间");
		label_6.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_6.setBounds(61, 232, 93, 18);
		getContentPane().add(label_6);

		txtSrol = new JTextField();
		txtSrol.setBounds(144, 232, 131, 24);
		getContentPane().add(txtSrol);
		txtSrol.setColumns(10);

		JButton Cancel = new JButton("取消");
		Cancel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		Cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 取消
				AddStudent.this.dispose();
			}
		});
		Cancel.setBounds(28, 272, 113, 27);
		getContentPane().add(Cancel);

		JButton SaveUpdate = new JButton("确认添加");
		SaveUpdate.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		SaveUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 添加学生信息
				String uuid =UUID.randomUUID().toString();
				// 一、取文本框数据
				String sid = txtSid.getText();
				String sname = txtSname.getText();
				String sgender = txtSgender.getText();
				String scollege = txtScollege.getText();
				String smajor = txtSmajor.getText();
				String sclass = txtSclass.getText();
				String senrol = txtSrol.getText();
				//二、封装成一个对象
				Student student = new Student(uuid,sid,sname,sgender,scollege,smajor,sclass,senrol);
				//三、调业务层
				UserService add = new UserServiceImpl();
				add.addStudent(student);
				//关闭本对话框
				AddStudent.this.dispose();
				//刷新数据
				frm.bindJTable();
			}
		});
		SaveUpdate.setBounds(188, 272, 131, 27);
		getContentPane().add(SaveUpdate);
	}
}
